#pragma once

#include "DXAMLCommon.h"
#include "XamlParserContext.h"
#include "XamlTextReaderSettings.h"
#include "XamlScannerNode.h"
#include "IPALReader.h"
#include "XamlQualifiedName.h"
#include "XamlPropertyName.h"
#include "XamlText.h"
#include "XamlCandidateIgnorableAttribute.h"
#include "XamlScannerStack.h"
#include "XamlCachedAttribute.h"
#include "XamlSortedAttributes.h"




class XamlScanner
{

public: 
	XamlScanner(const xshared_ptr<XamlParserContext> & spParserContext, struct IPALReader * pXmlReader, const xshared_ptr<XamlTextReaderSettings> & spSettings);
	~XamlScanner();
	HRESULT Init();
	HRESULT Read();
	HRESULT Peek(enum XamlScannerNode::ScannerNodeType & sntNextNodeType, xshared_ptr<XamlType> & spNextType);
	const XamlScannerNode & get_CurrentNode();
	XBOOL FoundXKeyDirective();
private: 
	HRESULT LoadQueue();
	HRESULT DoXmlRead();
	HRESULT PullXmlNode();
	HRESULT ReadElement();
	HRESULT ReadAttribute(const XamlScannerNode & elementNode);
	HRESULT ReadEndElement();
	HRESULT ReadTextOrWhitespace(XBOOL bIsWhitespace);
	HRESULT ReadPrefix(xshared_ptr<CXString> & outPrefix);
	HRESULT ReadLocalName(xshared_ptr<CXString> & outLocalName);
	HRESULT CacheAttributePrefixAndLocalName(const xshared_ptr<CXString> & inPrefix, const xshared_ptr<CXString> & inName);
	HRESULT GetCachedAttributePrefixAndLocalName(xshared_ptr<CXString> & inPrefix, xshared_ptr<CXString> & inName);
	void ClearAttributeNameCache();
	HRESULT PushNodeToQueue(const XamlScannerNode & inNode);
	HRESULT EnqueueTextNode();
	HRESULT PrivateReadText(XCHAR * * ppBuffer, XUINT32 * cBuffer);
	HRESULT PrivateReadText(xshared_ptr<CXString> & outssText);
	HRESULT PrivateReadTextInitializeXamlText(const xshared_ptr<XamlText> & inXamlText);
	HRESULT ReadPropertyElement(const xshared_ptr<XamlPropertyName> & inPropertyName, const xshared_ptr<XamlType> & inCurrentType, XBOOL bIsEmptyTag, XBOOL & outbIsElementIgnored);
	HRESULT VerifyNoAttributesOnPropertyElement();
	HRESULT ReadObjectElement(const xshared_ptr<XamlQualifiedName> & inName, XBOOL bIsEmptyTag, XBOOL & outbIsElementIgnored);
	HRESULT ReadObjectElement_DirectiveProperty(const xshared_ptr<XamlProperty> &, XamlScannerNode &);
	HRESULT ReadObjectElement_Object(const xshared_ptr<CXString> & inPrefix, const xshared_ptr<XamlNamespace> & inNamespace, const xshared_ptr<CXString> & inName, XBOOL bIsEmptyTag, XBOOL & outbIsElementIgnored);
	HRESULT ProcessAttributes(const XamlScannerNode & elementNode);
	HRESULT PreProcessAttributes();
	HRESULT AddNamespaceFromCurrentAttributeIfExists();
	HRESULT SkipNodesIfNamespaceIsIgnored(const xshared_ptr<XamlNamespace> & inNamespace, XBOOL bIsEmptyTag, XBOOL & outbIsIgnored);
	HRESULT SkipIgnoredElement(XBOOL bIsEmptyTag);
	HRESULT ValidateNamespacePrefixAndSkipIgnoredNodes(const xshared_ptr<CXString> & inPrefix, xshared_ptr<XamlNamespace> & outNamespace, XBOOL bIsEmptyTag, XBOOL & outbIsIgnored);
	HRESULT SkipIgnoredNodes(const xshared_ptr<CXString> & inPrefix, const xshared_ptr<XamlNamespace> & inNamespace, XBOOL bIsEmptyTag, XBOOL & outbIsIgnored);
	HRESULT GetErrorService(xshared_ptr<ParserErrorReporter> & outErrorService);
	HRESULT ReportXmlLiteError(HRESULT errorHR);
	HRESULT ReportError(XUINT32 errorCode, const xshared_ptr<CXString> & inssParam1, const xshared_ptr<CXString> &);
	HRESULT ReportError(XUINT32 errorCode, const xshared_ptr<CXString> & inssParam1);
	HRESULT ReportError(XUINT32 errorCode);
	HRESULT ReportUndeclaredNamespacePrefixError(const xshared_ptr<CXString> & inssPrefix);
	XBOOL HaveAccumulatedText();
	static const XUINT32 STARTING_STRING_BUILDER_SIZE;
	xshared_ptr<XamlParserContext> m_ParserContext;
	xshared_ptr<XamlText> m_AccumulatedText;
	xshared_ptr<XamlTextReaderSettings> m_spReaderSettings;
	xvector<XamlCandidateIgnorableAttribute> m_CandidateIgnorablePrefixes;
	XamlLineInfo m_WorkingLineInfo;
	XamlScannerStack m_ScannerStack;
	xqueue<XamlScannerNode> m_ScannerQueue;
	xvector<XamlCachedAttribute> m_AttributePrefixNameCache;
	xvector<XamlCachedAttribute>::iterator m_AttributePrefixNameCacheIterator;
	XamlScannerNode m_CurrentNode;
	XamlScannerNode m_WorkingNode;
	XamlSortedAttributes m_Attributes;
	struct IPALReader * m_XmlReader;
	XBOOL m_bIsPreprocessingAttributes;
	XBOOL m_bFoundFirstStartElement;
	XBOOL m_bQuirkShouldAllowSpacePreserveLeak;


};


